import * as fs from 'fs';

/**
 * 列出目标目录下的所有文件，不包括子目录下的文件
 * @param dirpath 目标目录
 */
export function listFileAndPath(dirpath:string):string[] {
  const fsstats = fs.statSync(dirpath);

  if (!fsstats.isDirectory()) {
    throw new Error(`${dirpath} is not a directory`);
  }

  const dir:fs.Dir = fs.opendirSync(dirpath);
  let filenames:string[] = [];
  try{
    let dirent:fs.Dirent;
    
    while(dirent = dir.readSync()) {
      if (dirent.isFile()) {
        filenames.push(dirent.name);
      } else {
        continue;
      }
    }
  } finally {
    dir.close();
  }
  
  return filenames;
}